//
//  LookPhotoViewController.m
//  RONGAO
//
//  Created by make on 2017/3/1.
//  Copyright © 2017年 wei wang. All rights reserved.
//

#import "LookPhotoViewController.h"
#import <QuartzCore/QuartzCore.h>
#import "DTCSharePlatformManager.h"
@interface LookPhotoViewController ()<UIScrollViewDelegate>
{
    NSInteger currentIndex; //当前图片下标
}
@property (weak, nonatomic) IBOutlet UIScrollView *bigScrollView;
@property (weak, nonatomic) IBOutlet UIScrollView *smallScrollView;
@property (weak, nonatomic) IBOutlet UIView *bottomView;
/** 图片数组 **/
@property(nonatomic,strong) NSMutableArray *photoArray;
@property (nonatomic,retain) NSString *photoFileName;


#define SHARE_ACTION_SHEET_TAG  1
#define DELETE_ACTION_SHEET_TAG 2

#define ALERT_TAG_LOGOUT_AUTH   100

@end

@implementation LookPhotoViewController

- (NSMutableArray *)photoArray{
    
    if (!_photoArray) {
        
        _photoArray = [NSMutableArray array];
        FMResultSet *rs = [[AppDelegate globalDelegate].database executeQuery:@"SELECT * FROM snapshot WHERE dev_uid=?", [AppDelegate globalDelegate].cameraModel.uid];
        
        while([rs next]) {
            
            NSString *filePath = [rs stringForColumn:@"file_path"];
            NSLog(@"photoArray file_path:%@", filePath);
            [_photoArray addObject:[NSString stringWithFormat:@"%@", filePath]];
        }
        [rs close];
        
        
    }
    return _photoArray;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    [self setNavigationStyle];
    [self configBigScrollView];
}


- (void)viewDidDisappear:(BOOL)animated{
    
    [super viewDidDisappear:animated];
    self.hidesBottomBarWhenPushed = NO;

}
#pragma mark - configUI
- (void)setNavigationStyle{
    [self setNavigationTitle:@"照片查看" titleColor:[UIColor colorWithRed:210/255.0 green:177/255.0 blue:104/255.0 alpha:1]];
    [self setLeftButtonImage:[UIImage imageNamed:@"icon_back_nor"]];
   
}


-(void)configBigScrollView
{
    NSString *albumDirectoryPath = self.directoryPath;
    NSArray *ary = self.photoArray;
    
    
    self.bigScrollView.contentSize = CGSizeMake(SCREEN_WIDTH * ary.count, 0);
    for (int i = 0; i< self.photoArray.count; i++) {
        UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(i*SCREEN_WIDTH, 0, SCREEN_WIDTH, self.bigScrollView.frame.size.height)];
        imageView.contentMode = UIViewContentModeScaleAspectFit;
        NSLog(@"=====%@",[NSString stringWithFormat:@"%@", ary[self.photoArray.count - i-1]]);
        imageView.image = [UIImage imageWithContentsOfFile:[albumDirectoryPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@", ary[self.photoArray.count - i-1]]]];
        [self.bigScrollView addSubview:imageView];
        imageView.tag =  i + 100;
    }
    
}

#pragma mark - delegate
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{

   
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    if (scrollView == _bigScrollView) {
        currentIndex = (int)(scrollView.contentOffset.x/(SCREEN_WIDTH-10));
        NSLog(@"滑动的currentIndex == %ld",(long)currentIndex);
        NSInteger count = self.photoArray.count - 1;
        
        self.photoFileName = self.photoArray[count - currentIndex];
    }
}

- (void)deleteSnapshotRecords:(NSString *)fileName {
    
    if ([AppDelegate globalDelegate].database != NULL) {
        
        FMResultSet *rs = [[AppDelegate globalDelegate].database executeQuery:@"SELECT * FROM snapshot WHERE file_path=?", self.photoFileName];
        
        NSFileManager *fileManager = [NSFileManager defaultManager];
        
        while([rs next]) {
            
            NSString *filePath = [rs stringForColumn:@"file_path"];
            [fileManager removeItemAtPath:[self pathForDocumentsResource: filePath] error:NULL];
            NSLog(@"camera(%@) snapshot removed", filePath);
        }
        
        [rs close];
        
        [[AppDelegate globalDelegate].database executeUpdate:@"DELETE FROM snapshot WHERE file_path=?", self.photoFileName];
        
    }
}

- (NSString *) pathForDocumentsResource:(NSString *) relativePath {
    
    static NSString* documentsPath = nil;
    
    if (nil == documentsPath) {
        
        NSArray* dirs = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        documentsPath = [dirs objectAtIndex:0];
    }
    
    return [documentsPath stringByAppendingPathComponent:relativePath];
}

- (IBAction)deletePhoto:(UIButton *)sender {
    
    if (self.photoFileName) {
        
    }
    
    NSInteger index = [self.photoArray indexOfObject:self.photoFileName];
    
    if (index > self.photoArray.count || index < 0) {
        index = self.photoArray.count -1;
        
        self.photoFileName = self.photoArray[index];
    }
    
    [self deleteSnapshotRecords:self.photoFileName];
    [self.photoArray removeObjectAtIndex:index];
    
    [SVProgressHUD showSuccessWithStatus:@"删除成功"];
    if ([self.photoArray count] > 0){
        
    
        [self configBigScrollView];
        
    }
    else {
        [self.navigationController popViewControllerAnimated:YES];
    }

}

- (IBAction)shareImgae:(UIButton *)sender {
    
    NSLog(@"分享图片");
    UIImageView *shareImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:self.photoFileName]];

    
    [DTCSharePlatformManager showInView:shareImage imageList:@[[DTCSharePlatformManager getImageByView:self.view]] success:^{
        
        [SVProgressHUD showSuccessWithStatus:@"分享成功"];
    } fail:^{

    }];

}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
